package com.xjtu.chapter09;

import com.xjtu.chapter08.thread.producer_consumer.Product;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/9/3 22:26
 */
public class MapTest {
    public static void main(String[] args) {
        Product t1 = new Product(11,"lenovo");
        Product t2 = new Product(12,"dell");
        Product t3= new Product(13,"mac");
        Map<Integer,String> map = new HashMap<>();
        map.put(t1.getId(),t1.getName());
        map.put(t2.getId(),t2.getName());
        map.put(t3.getId(),t3.getName());

        Set<Integer> set = map.keySet();
        System.out.println("Map集合中所有元素是：");
        Iterator<Integer>it = set.iterator();
        while (it.hasNext()){
            Integer key = (Integer)it.next();
            String name = (String)map.get(key);
            System.out.println(key+"——"+name);
        }
        map.remove(11);//将id为"11"的对象从集合中删除
        System.out.println("Map集合执行删除操作以后所有元素是：");
        Iterator<Integer>iterator = set.iterator();
        while (iterator.hasNext()){
            Integer key = (Integer)iterator.next();
            String name = (String)map.get(key);
            System.out.println(key+"——"+name);
        }
    }
}
